<?php

$pagesXML = new DOMDocument();
$pagesXML->load('data/pages.xml');


$xsl_dom = new DOMDocument();
$xsl_dom->load('get_array_pages.xsl');
$xslt = new XSLTProcessor();
$xslt->importStylesheet($xsl_dom);
$html = $xslt->transformToXml($pagesXML);
$pages = array();
eval($html);

$this_dir = dirname(__FILE__) . '/';
$root = $this_dir . 'pages/';


$xsl_template =
'<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output
    method="xml"
    version="1.0"
    encoding="US-ASCII"
    indent="yes" />

    <xsl:param name="host" />
    <xsl:param name="root-path" />

    <xsl:template match="text()" />

    <xsl:template match="/">
    </xsl:template>

</xsl:stylesheet>';


function create_page_files($root, $page)
{
    global $xsl_template;

    if (! file_exists($root.'data.xsl')) {
        $fp = fopen($root.'data.xsl', 'w');
        if (fwrite($fp, $xsl_template) === false) {
            echo "Can`t write to \"{$root}data.xsl\"<br />";
        }
        fclose($fp);
    }
}

function create_page_dir($root, $pages)
{
    foreach ($pages as $page_id => $page) {
        if (! file_exists($root.$page_id)) {
            mkdir($root . $page_id);
        }
        create_page_files($root.$page_id.'/', $page);
        if (isset($page['...'])) {
            create_page_dir($root . $page_id . '/', $page['...']);
        }
    }
}

if (! file_exists($root)) {
    mkdir($root);
}


create_page_dir($root, $pages);

?>